package com.example.consumptionrecords.util.sms;

import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.google.gson.Gson;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * hhzr9
 * 2024 \ 03 \ 31
 */

@Slf4j
@Component
public class SmsUtil {

	@Value("${ali.access-key-id}")
	private String accessKeyId;

	@Value("${ali.access-key-secret}")
	private String accessKeySecret;

	public com.aliyun.dysmsapi20170525.Client createClient() throws Exception, TeaException {
		// 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考。
		// 建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html。
		com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
				// 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
				.setAccessKeyId(accessKeyId)
				// 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
				.setAccessKeySecret(accessKeySecret);
		// Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
		config.endpoint = "dysmsapi.aliyuncs.com";
		return new com.aliyun.dysmsapi20170525.Client(config);
	}


	public void sendSms(String phone, String code, String templateCode) throws Exception {
		com.aliyun.dysmsapi20170525.Client client = this.createClient();
		com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
				.setSignName("CR")
				.setTemplateCode(templateCode)
				.setPhoneNumbers(phone)
				.setTemplateParam("{\"code\":\"" + code + "\"}");
		com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
		SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
		log.info("请求发送短信状态码：{}", sendSmsResponse.getStatusCode());
		log.info("请求发送短信返回信息code：{}", sendSmsResponse.getBody().getCode());
		log.info("请求发送短信返回信息message：{}", sendSmsResponse.getBody().getMessage());
	}
}
